package com.ctg.itrdc.sysmgr.portal.service.aop;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.CommonRegionDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.OrganizationDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PlatInfoDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ICommonRegionService;
import com.ctg.itrdc.sysmgr.portal.api.service.IOrganizationService;
import com.ctg.itrdc.sysmgr.portal.api.service.IPlatInfoService;
import com.ctg.itrdc.sysmgr.portal.api.service.IStaffService;

/**
 * @author blackfox 对象翻译处理
 * @param <T>
 */
@Aspect
@Component
public class TranslateAOP {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private IOrganizationService organizationService;
	@Resource
	private ICommonRegionService commonRegionService;
	@Resource
	private IStaffService staffService;
	@Resource
	private IPlatInfoService platInfoService;

	@Pointcut(value = "execution(* queryPage*(..))"
			+ "|| execution(* com.ctg.itrdc.sysmgr.portal.service.impl.SystemUserServiceImpl.selectByOrgId(..))")
	private void pagePointcut() {
	}

	@Pointcut(value = "execution(* selectByPrimaryKey(..))")
	private void singlePointcut() {
	}

	// @Pointcut(value = "execution(* selectByExample(..))"
	// + "||execution(* selectByOrgIdAndRzOrgId(..))")
	// private void listPointcut() {
	// }

	/**
	 * 分页翻译处理
	 * 
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * 
	 */
	@AfterReturning(value = "pagePointcut()", returning = "retVal")
	public void page(JoinPoint joinPoint, Object retVal)
			throws Exception {

		PageInfo<?> page = (PageInfo<?>) retVal;
		List<?> list = page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Object object : list) {
				setOrg(object);
				setCommonRegion(object);
				setStaff(object);
				setPlatInfo(object);
			}
		}
	}

	/**
	 * get翻译处理
	 * 
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws InstantiationException 
	 * 
	 */
	@AfterReturning(value = "singlePointcut()", returning = "retVal")
	public void single(JoinPoint joinPoint, Object retVal)
			throws Exception {
		setOrg(retVal);
		setCommonRegion(retVal);
		setStaff(retVal);
		setPlatInfo(retVal);
	}

	/**
	 * 列表翻译处理
	 * 
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * 
	 */
	// @AfterReturning(value = "listPointcut()", returning = "retVal")
	// private void list(JoinPoint joinPoint, Object retVal)
	// throws IllegalAccessException, InvocationTargetException,
	// NoSuchMethodException {
	// List<?> list = (List<?>) retVal;
	// setOrgList(list);
	// setCommonRegionList(list);
	// setStaffList(list);
	// }

	private void setCommonRegion(Object retVal) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		if (retVal instanceof StaffDTO || retVal instanceof SystemPostDTO
				|| retVal instanceof SystemRolesDTO
				|| retVal instanceof SystemUserDTO) {
			String idStr = BeanUtils.getProperty(retVal, "commonRegionId");
			if (StringUtils.isNotBlank(idStr)) {
				long id = Long.parseLong(idStr);
				CommonRegionDTO dto = new CommonRegionDTO();
				dto.setCommonRegionId(id);
				dto = commonRegionService.selectByPrimaryKey(dto);
				if (dto != null)
					BeanUtils.setProperty(retVal, "commonRegion", dto);
			}
		}
	}

	private void setOrg(Object retVal) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		if (retVal instanceof StaffDTO || retVal instanceof SystemPostDTO) {
			String idStr = BeanUtils.getProperty(retVal, "orgId");
			if (StringUtils.isNotBlank(idStr)) {
				long id = Long.parseLong(idStr);
				OrganizationDTO organization = new OrganizationDTO();
				organization.setId(id);
				organization = organizationService
						.selectByPrimaryKey(organization);
				if (organization != null)
					BeanUtils.setProperty(retVal, "organization", organization);
			}
		}
	}

	private void setStaff(Object retVal) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		if (retVal instanceof SystemUserDTO) {
			String idStr = BeanUtils.getProperty(retVal, "staffId");
			if (StringUtils.isNotBlank(idStr)) {
				long id = Long.parseLong(idStr);
				StaffDTO dto = new StaffDTO();
				dto.setStaffId(id);
				System.out.println("staffService: " + staffService + ", dto: " + dto);
				dto = staffService.selectByPrimaryKey(dto);
				if (dto != null)
					BeanUtils.setProperty(retVal, "staff", dto);
			}
		}
	}

	private void setPlatInfo(Object retVal) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException, InstantiationException {
		if (retVal instanceof SystemUserDTO || retVal instanceof SystemPostDTO
				|| retVal instanceof SystemRolesDTO) {
			String sysCode = BeanUtils.getProperty(retVal, "sysCode");
			if (StringUtils.isNotBlank(sysCode)) {
				PlatInfoDTO dto = platInfoService.selectByPlatSysNbr(sysCode);
				if (dto != null)
					BeanUtils.setProperty(retVal, "platInfo", BeanUtils.cloneBean(dto));
			}
		}
	}

}
